package com.qf.service.impl;

import com.qf.api.CacheFegin;
import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.CheckMobileOperator;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import javafx.scene.chart.ValueAxis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 号码补齐策略
 */
@Service("phase")
@Slf4j
public class PhaseFilterServiceImpl implements FilterChainService {

    @Resource
    private CacheFegin cacheFegin;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) throws Exception {
        //获取手机号
        String destMobile = standardSubmit.getDestMobile();
        //判断所属
        if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);//移动
            standardSubmit.setGatewayID(OperatorConstants.CHINA_MOBILE);
        }else  if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);//电信
            standardSubmit.setGatewayID(OperatorConstants.CHINA_TELECOM);
        }else  if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);//联通
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNICOM);
        }else  if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);//未知
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
        }
        String key= CacheConstants.CACHE_PREFIX_PHASE+ destMobile.substring(0,7);
        String value =cacheFegin.get(key);
        if (StringUtils.isEmpty(value)){
            standardSubmit.setProvinceId(0);
            standardSubmit.setCityId(0);
        }else {
            String[] split = value.split(",");
            standardSubmit.setProvinceId(Integer.parseInt(split[0]));
            standardSubmit.setCityId(Integer.parseInt(split[1]));
        }


        Map routerMap = cacheFegin.hMGet(CacheConstants.CACHE_PREFIX_ROUTER +
                standardSubmit.getClientID() + ":" +
                standardSubmit.getGatewayID());
        Map channelMap = cacheFegin.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL +
                standardSubmit.getClientID() + ":" +
                standardSubmit.getGatewayID());
        if(routerMap !=null&& channelMap!=null){
            String srcNumber=String.valueOf(channelMap.get("spnumber"))+ String.valueOf(routerMap.get("extendnumber"));
            standardSubmit.setSrcNumber(srcNumber);
            standardSubmit.setFee(Long.valueOf(Integer.valueOf(String.valueOf(routerMap.get("price")))));
            log.info("号码补全策略执行成功");
        }


        return true;
    }


}
